package com.gitee.zhangchenyan.takin.config;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.listener.impl.PropertiesListener;
import lombok.Cleanup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.StringReader;
import java.util.Properties;

/**
 * 配置文件定时任务
 */
@Slf4j
@Component
public class ConfigScheduleListener implements EnvironmentPostProcessor {


    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        try {
            String nacosAddr = environment.getProperty("takin.nacosServerAddress");
            ConfigService configService = NacosFactory.createConfigService(nacosAddr);
            //初始化公共配置
            initConfig(configService, environment, "public", "DEFAULT_GROUP");
            //初始化application配置
            String applicationDataId = environment.getProperty("spring.application.name");
            initConfig(configService, environment, applicationDataId, "DEFAULT_GROUP");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("初始化配置异常:" + e.toString());
        }
    }

    @SneakyThrows
    private void initConfig(ConfigService configService, ConfigurableEnvironment environment, String dataId, String group) {
        Properties publicProperties = getNacosConfig(configService, dataId, group);
        environment.getPropertySources().addLast(new PropertiesPropertySource(dataId + "_" + group, publicProperties));
        configService.addListener(dataId, group, new PropertiesListener() {
            @Override
            public void innerReceive(Properties properties) {
                log.info("刷新" + dataId + "_" + group + "配置");
                if (properties != null && properties.size() > 0) {
                    environment.getPropertySources().addLast(new PropertiesPropertySource(dataId + "_" + group, properties));
                }
            }
        });
    }

    @SneakyThrows
    private Properties getNacosConfig(ConfigService configService, String dataId, String group) {
        String config = configService.getConfig(dataId, group, 60);
        Properties properties = new Properties();
        @Cleanup StringReader stringReader = new StringReader(config);
        properties.load(stringReader);
        return properties;
    }

}
